package com.aegis.common.redis.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author wx
 */
@ComponentScan({"com.aegis.common.redis"})
@Configuration
public class AutoRedisConfig {


    /**
     * @param factory
     * @return
     */
    @Bean("aegisRedisTemplate")
    public RedisTemplate<String, Object> custRedisTemplate(RedisConnectionFactory factory) {
        // 初始化序列化器
        StringRedisSerializer keySerializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer valueSerializer = new GenericJackson2JsonRedisSerializer();

        // 配置ObjectRedisTemplate
        RedisTemplate<String, Object> objectRedisTemplate = new RedisTemplate<>();
        objectRedisTemplate.setConnectionFactory(factory);
        objectRedisTemplate.setKeySerializer(keySerializer);
        objectRedisTemplate.setHashKeySerializer(keySerializer);
        objectRedisTemplate.setValueSerializer(valueSerializer);
        objectRedisTemplate.setHashValueSerializer(valueSerializer);
        objectRedisTemplate.afterPropertiesSet();

        return objectRedisTemplate;
    }

}